﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;
using Antu.Utilities;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_AddPCHRYHZControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        Label lbl征收批次号;
        TextBox 补偿总, 补助, 已划转, txt余额, txt划转金额, txt备注, txtPCID, txtID, txtURID, txtPNID, 产权补助, 货币补助, txtZSUSERID, txt提交状态;
        MPCalendar txt划转时间;

        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();

            string strID = base.Page.Request["ID"];
            string strPCID = base.Page.Request["PCID"];
            string strPNID = base.Page.Request["PNID"];
            string personid = base.Page.Request["personid"];

            Button btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);

            if (!base.Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(strPCID)) { txtPCID.Text = strPCID; }
                if (!string.IsNullOrEmpty(strPNID)) { txtPNID.Text = strPNID; }
                if (!string.IsNullOrEmpty(personid)) { 
                    txtZSUSERID.Text = personid; 
                    
                }
                if (HttpContext.Current.Session["UserID"] != null) { txtURID.Text = HttpContext.Current.Session["UserID"].ToString(); }
                if (!string.IsNullOrEmpty(strID)) { 
                    txtID.Text = strID;
                    EluZSProvider.Zspch zspch = _EluZSDataProvider.GetZspch(strID);
                    txt余额.Text = zspch.余额;
                    txt划转金额.Text = zspch.划转金额;
                    txt备注.Text = zspch.备注;
                    txtPCID.Text = zspch.PCID;
                    txtURID.Text = zspch.URID;

                    txtPNID.Text = zspch.PNID;
                    txtZSUSERID.Text = zspch.ZSUSERID;
                    txt提交状态.Text = zspch.提交状态;
                    lbl征收批次号.Text = zspch.征收批次号;
                    if (zspch.划转时间.HasValue) { txt划转时间.Text = zspch.划转时间.Value.ToShortDateString(); }
                }
                getData();
                if (getFirst())
                {
                    txt余额.Text = returnData1().ToString();
                }
                else
                {
                    txt余额.Text = returnData3().ToString();
                }
            }
        }
        protected void InitControl() {
            lbl征收批次号 = (Label)FindControl("lbl征收批次号");
            补偿总 = (TextBox)FindControl("补偿总");
            补助 = (TextBox)FindControl("补助");
            已划转 = (TextBox)FindControl("已划转");
            txt余额 = (TextBox)FindControl("txt余额");

            txt划转金额 = (TextBox)FindControl("txt划转金额");
            txt备注 = (TextBox)FindControl("txt备注");
            txtPCID = (TextBox)FindControl("txtPCID");
            txtID = (TextBox)FindControl("txtID");
            txtURID = (TextBox)FindControl("txtURID");

            txtPNID = (TextBox)FindControl("txtPNID");
            产权补助 = (TextBox)FindControl("产权补助");
            货币补助 = (TextBox)FindControl("货币补助");
            txtZSUSERID = (TextBox)FindControl("txtZSUSERID");
            txt划转时间 = (MPCalendar)FindControl("txt划转时间");

            txt提交状态 = (TextBox)FindControl("txt提交状态");
        }
        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            string strID = base.Page.Request["ID"];
            string strPCID = base.Page.Request["PCID"];
            string strPNID = base.Page.Request["PNID"];
            string personid = base.Page.Request["personid"];

            EluZSProvider.Zspch zspch = _EluZSDataProvider.GetZspch(strID);
            zspch.余额 = txt余额.Text;
            zspch.划转金额 = txt划转金额.Text;
            zspch.备注 = txt备注.Text;
            zspch.PCID = txtPCID.Text;
            zspch.URID = txtURID.Text;

            zspch.PNID = txtPNID.Text;
            zspch.ZSUSERID = txtZSUSERID.Text;
            zspch.划转时间 = (string.IsNullOrEmpty(txt划转时间.Text) ? null : (DateTime?)Convert.ToDateTime(txt划转时间.Text));
            //判断是否超出标准
            string temp = txt划转金额.Text.Trim();
            double tt1 = 0;
            if (!string.IsNullOrEmpty(temp)) { tt1 = Convert.ToDouble(temp); }
            if (tt1 != gethzjz())
            {
                if (tt1 > gethzjz())
                {
                    double temp2 = tt1 - gethzjz();
                    double temp3 = returnData3() - temp2;
                    if (temp3 < 0)
                    {
                        UtilitiesPage.AlertDialog("实际划转额度已超过应补偿总金额！", "window.location.href=window.location.href;");
                    }
                    else
                    {
                        zspch.余额 = temp3.ToString();
                        bool save = _EluZSDataProvider.SaveZspch(strID, zspch);
                        if (save) { UtilitiesPage.AlertDialog("保存成功！", "window.location.href('ZS_AddPCHRYHZ.aspx?ID=" + txtID.Text + "&PCID=" + strPCID + "&PNID=" + strPNID + "&personid=" + personid + "&type=');");}
                    }
                }
                else
                {
                    double temp1 = gethzjz() - tt1;
                    zspch.余额 = temp1.ToString();
                    bool save = _EluZSDataProvider.SaveZspch(strID, zspch);
                    if (save) { UtilitiesPage.AlertDialog("保存成功！", "window.location.href('ZS_AddPCHRYHZ.aspx?ID=" + txtID.Text + "&PCID=" + strPCID + "&PNID=" + strPNID + "&personid=" + personid + "&type=');"); }
                }
            }
            else
            {
                UtilitiesPage.AlertDialog("保存成功！", "window.location.href('ZS_AddPCHRYHZ.aspx?ID=" + txtID.Text + "&PCID=" + strPCID + "&PNID=" + strPNID + "&personid=" + personid + "&type=');");
            }
        }
        /// <summary>
        /// 获取其划转金额
        /// </summary>
        /// <returns></returns>
        protected double gethzjz()
        {
            double tt1 = 0;
            string strID = base.Page.Request["ID"];
            string temp = _EluZSDataProvider.GetSomeStr("zs_zspch", "划转金额", "id", strID);
            if (!string.IsNullOrEmpty(temp)) { tt1 = Convert.ToDouble(temp); }
            return tt1;
        }
        /// <summary>
        /// 绑定字段
        /// </summary>
        protected void getData()
        {
            string strPerID = base.Page.Request["personid"];
            if (!string.IsNullOrEmpty(strPerID))
            {
                产权补助.Text = _EluZSDataProvider.GetGZJE(strPerID, "", "1");
                if (string.IsNullOrEmpty(产权补助.Text)) { 产权补助.Text = "0"; }
                货币补助.Text = _EluZSDataProvider.GetGZJE(strPerID, "", "2");
                if (string.IsNullOrEmpty(货币补助.Text)) { 货币补助.Text = "0"; }
                补偿总.Text = _EluZSDataProvider.GetGZJE(strPerID, "", "3");
                if (string.IsNullOrEmpty(补偿总.Text)) { 补偿总.Text = "0"; }
                已划转.Text = _EluZSDataProvider.GetGZJE(strPerID, base.Page.Request["PNID"], "4");
                if (string.IsNullOrEmpty(已划转.Text)) { 已划转.Text = "0"; }
            }
            returnData();
        }
        /// <summary>
        /// 是否首次划转
        /// </summary>
        /// <returns></returns>
        protected bool getFirst()
        {
            string strID = base.Page.Request["ID"];
            string temp = _EluZSDataProvider.GetSomeStr("zs_zspch", "划转金额", "id", strID);
            if (string.IsNullOrEmpty(temp)) { return true; } else { return false; }
        }
        protected double returnData()
        {
            double tt1 = 0;
            double tt2 = 0;
            if (!string.IsNullOrEmpty(产权补助.Text)) { tt1 = Convert.ToDouble(产权补助.Text); }
            if (!string.IsNullOrEmpty(货币补助.Text)) { tt2 = Convert.ToDouble(货币补助.Text); }
            double tt = (tt1 + tt2);
            补助.Text = tt.ToString();
            return tt;
        }
        protected double returnData1()
        {
            double tt1 = 0;
            double tt2 = 0;
            double tt3 = 0;
            double tt4 = 0;
            if (!string.IsNullOrEmpty(补助.Text)) { tt1 = Convert.ToDouble(补助.Text); }
            //if (!string.IsNullOrEmpty(补偿总.Text)) { tt2 = Convert.ToDouble(补偿总.Text); }
            if (!string.IsNullOrEmpty(已划转.Text)) { tt3 = Convert.ToDouble(已划转.Text); }
            if (!string.IsNullOrEmpty(txt划转金额.Text)) { tt4 = Convert.ToDouble(txt划转金额.Text.Trim()); }//本次划转金额
            //double tt = (tt1 + tt2) - (tt3 + tt4);
            double tt = tt1 - (tt3 + tt4);
            return tt;
        }
        protected double returnData2()
        {
            double tt1 = 0;
            double tt2 = 0;
            if (!string.IsNullOrEmpty(补助.Text)) { tt1 = Convert.ToDouble(补助.Text); }
            if (!string.IsNullOrEmpty(补偿总.Text)) { tt2 = Convert.ToDouble(补偿总.Text); }
            double tt = tt1 + tt2;
            return tt;
        }
        protected double returnData3()
        {
            double tt1 = 0;
            double tt2 = 0;
            double tt3 = 0;
            if (!string.IsNullOrEmpty(补助.Text)) { tt1 = Convert.ToDouble(补助.Text); }
            if (!string.IsNullOrEmpty(补偿总.Text)) { tt2 = Convert.ToDouble(补偿总.Text); }
            if (!string.IsNullOrEmpty(已划转.Text)) { tt3 = Convert.ToDouble(已划转.Text); }
            //double tt = (tt1 + tt2) - tt3;
            double tt = tt2 - tt3;
            return tt;
        }
    }
}


